﻿using Ags.Mvvm;
using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace Ags.MonsterTools.Guids
{
    public class GuidViewModel : ViewModelBase<GuidViewModel>
    {
        public GuidViewModel()
        {
            GenerateGuidsCommand = new DelegateCommand(ExecuteGenerateGuidsCommand, CanExecuteGenerateGuidsCommand);
            CopyClipboardCommand = new DelegateCommand<object>(ExecuteCopyClipboardCommand);
        }

        private int _amount;
        public int Amount
        {
            get { return _amount; }
            set
            {
                _amount = value;
                OnPropertyChanged();
                GenerateGuidsCommand.RaiseCanExecuteChanged();
            }
        }

        private ObservableCollection<Guid> _guidList;
        public ObservableCollection<Guid> GuidList
        {
            get
            {
                if (_guidList == null)
                    _guidList = new ObservableCollection<Guid>();
                return _guidList;
            }
            set
            {
                _guidList = value;
                OnPropertyChanged();
            }
        }

        public DelegateCommand GenerateGuidsCommand { get; private set; }

        void ExecuteGenerateGuidsCommand()
        {
            GuidList.Clear();
            var amount = Amount;
            while (amount-- > 0)
                GuidList.Add(Guid.NewGuid());
        }

        bool CanExecuteGenerateGuidsCommand()
        {
            return Amount > 0;
        }

        public DelegateCommand<object> CopyClipboardCommand { get; private set; }

        void ExecuteCopyClipboardCommand(object parameter)
        {
            var val = parameter.ToString();
            Clipboard.SetText(val);
        }

        public override void Dispose()
        {
            this.GuidList.Clear();
            this.GuidList = null;
        }
    }
}
